package cc.rengu.redp.bizimpl.igas.controller;


import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.igas.domain.PTxnCfg;
import cc.rengu.redp.bizimpl.igas.domain.PDstChannelInfo;
import cc.rengu.redp.bizimpl.igas.service.PBankSupportCfgService;
import cc.rengu.redp.bizimpl.igas.service.PTxnCfgService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-09
 */
@Slf4j
@Api(tags = "交易信息配置管理")
@RestController
@RequestMapping("/admin/igas/pTxnCfg")
public class PTxnCfgController extends BaseController {

    @Autowired
    private PTxnCfgService pTxnCfgService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, PTxnCfg entity) {
        return getDataTable(this.pTxnCfgService.findByPage(request, entity));
    }

    @GetMapping("getOne")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "txnNum", value = "内部交易码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bizType", value = "业务细分", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "txnGroup", value = "交易分组", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bizProdCode", value = "业务产品代码", required = true, dataType = "String")
    })
    public PTxnCfg getOne(String txnNum,String bizType, String txnGroup, String bizProdCode) throws RedpException {
      return this.pTxnCfgService.findOne(txnNum,bizType,txnGroup,bizProdCode);
    }

    @PostMapping(value = "/edit")
    public void editFromChnl(@ModelAttribute("pSecPlanInfo") PTxnCfg entity) throws RedpException {
        PTxnCfg cfg = pTxnCfgService.findOne(entity.getTxnNum(), entity.getBizType(), entity.getTxnGroup(), entity.getBizProdCode());
        if (Common.isNotBlank(cfg)) {
            entity.setUpdateTime(DateUtils.formatDate(new Date(),DateUtils.DEFAULT_TIME_FORMAT+".SSS"));
            LambdaUpdateWrapper<PTxnCfg> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PTxnCfg::getTxnNum, entity.getTxnNum());
            wrapper.eq(PTxnCfg::getBizType, entity.getBizType());
            wrapper.eq(PTxnCfg::getTxnGroup, entity.getTxnGroup());
            wrapper.eq(PTxnCfg::getBizProdCode, entity.getBizProdCode());
            pTxnCfgService.update(entity, wrapper);
        } else {
            entity.setCreateTime(DateUtils.formatDate(new Date(),DateUtils.DEFAULT_TIME_FORMAT+".SSS"));
            pTxnCfgService.save(entity);
        }
    }

    @PostMapping(value = "/changeStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "txnNum", value = "内部交易码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bizType", value = "业务细分", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "txnGroup", value = "交易分组", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bizProdCode", value = "业务产品代码", required = true, dataType = "String")
    })
    public void changeStatus(PTxnCfg entity) throws RedpException {

        PTxnCfg db = pTxnCfgService.findOne(entity.getTxnNum(), entity.getBizType(), entity.getTxnGroup(), entity.getBizProdCode());
        ;
        if (Common.isBlank(db)) {
            log.error("根据唯一索引没有找到指定数据！");
            throw new RedpException("没有找到指定数据！");
        }
        if(IgasYesOrNoEnum.Yes.getType().equals(entity.getSupportFlag())
                ||IgasYesOrNoEnum.No.getType().equals(entity.getSupportFlag())){

            db.setSupportFlag(entity.getSupportFlag());
            db.setUpdateTime(DateUtils.formatDate(new Date(),DateUtils.DEFAULT_TIME_FORMAT+".SSS"));
            LambdaUpdateWrapper<PTxnCfg> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PTxnCfg::getTxnNum, entity.getTxnNum());
            wrapper.eq(PTxnCfg::getBizType, entity.getBizType());
            wrapper.eq(PTxnCfg::getTxnGroup, entity.getTxnGroup());
            wrapper.eq(PTxnCfg::getBizProdCode, entity.getBizProdCode());
            pTxnCfgService.update(db, wrapper);
        }
    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "查询条件", required = true, dataType = "String")
    })
    @GetMapping(value = "/pTxnCfgSelect2List")
    public List<PTxnCfg>  pTxnCfgSelect2List(String q) throws RedpException {
        if(q.indexOf("&amp;")>0){
            q=q.replaceAll("&amp;", "&");
        }
        String[] param = q.split("&");
        return pTxnCfgService.findPTxnCfgByList(param[0],null,null,null);
    }
}
